﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.MobileControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SimpleServiceBus.Bus;
using SimpleServiceBus.Messages.Management.Monitoring;

namespace SampleAsyncWebClient
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnPing_Click(object sender, EventArgs e)
        {
            RegisterAsyncTask(new PageAsyncTask(
               BeginRequest,
               EndRequest,
               null,
               null
               ));
        }

        private void EndRequest(IAsyncResult ar)
        {
            var response = ar.AsyncState as CompletionResult;
            if (response == null)
                return;

            var pingResponse = (EndpointPingResponse) response.Message;
            Response.Write(pingResponse.Message);
        }

        private IAsyncResult BeginRequest(object sender, EventArgs e, AsyncCallback cb, object extraData)
        {
            var message = txtMessage.Text;
            var destination = txtDestinationAddress.Text;

            var request = new EndpointPingRequest {Message = message};

            return Global.Endpoint.MessageBus.Send(request,destination).Register(cb, null);
        }
    }
}
